//----------------------------------Include------------------------------------
#include "mcu.h"
#include "glbvar.h"
//-----------------------------------------------------------------------------

//DMA:  8 channel
//DMA Sources map
//      srccfg=0    1               2                 6         7
//ch0   rsvd        eUSCI_A0 TX     eUSCI_B0 TX0      TA0CCR0   AES256_Trigger0
//ch1   rsvd        eUSCI_A0 RX     eUSCI_B0 RX0      TA0CCR2   AES256_Trigger1
//ch2   rsvd        eUSCI_A1 TX     eUSCI_B1 TX0      TA1CCR0   AES256_Trigger2
//ch3   rsvd        eUSCI_A1 RX     eUSCI_B1 RX0      TA1CCR2   rsvd
//ch4   rsvd        eUSCI_A2 TX     eUSCI_B2 TX0      TA2CCR0   rsvd
//ch5   rsvd        eUSCI_A2 RX     eUSCI_B2 RX0      TA2CCR2   rsvd
//ch6   rsvd        eUSCI_A3 TX     eUSCI_B3 TX0      TA3CCR0   DMAE0(External Pin)
//ch7   rsvd        eUSCI_A3 RX     eUSCI_B3 RX0      TA3CCR2   ADC

//ADC:  16bits  1Msps  SAR  24channel
//ADC Trigger Signal Connections
//
//0 software
//1 TA0C1   2 TA0C2   3 TA1_C1   4 TA1_C2   5 TA2_C1   6 TA2_C2   7 TA3_C1

//-----------------------------------Macro-------------------------------------
#define _DMA_INT            DMA_INT2
#define _INT_DMA_INT        INT_DMA_INT2
//-----------------------------------------------------------------------------
#define _SAMP_LEN           GM_SAMP_LEN
#define _ADC_VREF           (3.3)
//-----------------------------------------------------------------------------

//--------------------------------Static Var-----------------------------------
#if 0
#if defined(__TI_COMPILER_VERSION__)
#pragma DATA_ALIGN(controlTable, 1024)
#elif defined(__IAR_SYSTEMS_ICC__)
#pragma data_alignment = 1024
#elif defined(__GNUC__)
__attribute__((aligned(1024)))
#elif defined(__CC_ARM)
__align(1024)
#endif
static uint8_t controlTable[256];       //>= 256
//-----------------------------------------------------------------------------
uint16_t    data_u0[_SAMP_LEN];
uint16_t    data_u1[_SAMP_LEN];
uint16_t    data_i0[_SAMP_LEN];
uint16_t    data_i1[_SAMP_LEN];
#endif
//-----------------------------------------------------------------------------
//uint16_t    data_u[_SAMP_LEN];
//uint16_t    data_i[_SAMP_LEN];
//--------------------------------Static Func----------------------------------
//-----------------------------------------------------------------------------

//--------------------------------Public Func----------------------------------
void mcu_init_adc(void)
{
//configure internal reference
  while( REF_A_isRefGenBusy() == 1 );
  REF_A_setReferenceVoltage(REF_A_VREF2_5V);
  REF_A_enableReferenceVoltageOutput();
  REF_A_enableReferenceVoltage();
  
//pin
  //P5.5 -- A0   P5.4 -- A1
  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN5, GPIO_PRIMARY_MODULE_FUNCTION);
  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN4, GPIO_PRIMARY_MODULE_FUNCTION);
  //P4.5 -- A8   P4.4 -- A9
  //GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4, GPIO_PIN5, GPIO_PRIMARY_MODULE_FUNCTION);
  //GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4, GPIO_PIN4, GPIO_TERTIARY_MODULE_FUNCTION);
  
//adc
  ADC14_enableModule();
  ADC14_initModule(ADC_CLOCKSOURCE_MCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_1, 0);   //Initializing ADC (MCLK/1/1)
  ADC14_setResolution(ADC_14BIT);
  ADC14_setResultFormat(ADC_UNSIGNED_BINARY);
  ADC14_setSampleHoldTime(ADC_PULSE_WIDTH_128, ADC_PULSE_WIDTH_128);
  ADC14_setSampleHoldTrigger(ADC_TRIGGER_SOURCE3, false);   //3 TA1_C1

//ADC14_configureSingleSampleMode(ADC_MEM0, true);
//ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS, ADC_INPUT_A10, false);
  ADC14_configureMultiSequenceMode(ADC_MEM0, ADC_MEM1, true);
  ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS, ADC_INPUT_A0,  false);
  ADC14_configureConversionMemory(ADC_MEM1, ADC_VREFPOS_AVCC_VREFNEG_VSS, ADC_INPUT_A1,  false);
  
//interrupt
  ADC14_enableInterrupt(ADC_INT1);
  Interrupt_enableInterrupt(INT_ADC14);

//start
  ADC14_enableConversion();

#if 0
//DMA
  DMA_enableModule();
  DMA_setControlBase(controlTable);   //controlTable is a pointer to the 1024-byte-aligned base address
  
  DMA_disableChannelAttribute(DMA_CH7_ADC14, UDMA_ATTR_ALTSELECT | UDMA_ATTR_USEBURST | UDMA_ATTR_HIGH_PRIORITY | UDMA_ATTR_REQMASK);
  DMA_assignChannel(DMA_CH7_ADC14);
  
  DMA_setChannelControl (UDMA_PRI_SELECT | DMA_CH7_ADC14, UDMA_SIZE_16 | UDMA_SRC_INC_NONE | UDMA_DST_INC_16 | UDMA_ARB_1);
  DMA_setChannelTransfer(UDMA_PRI_SELECT | DMA_CH7_ADC14, UDMA_MODE_PINGPONG, (void*) &ADC14->MEM[0], data_u0, GM_SAMP_LEN);
  DMA_setChannelControl (UDMA_ALT_SELECT | DMA_CH7_ADC14, UDMA_SIZE_16 | UDMA_SRC_INC_NONE | UDMA_DST_INC_16 | UDMA_ARB_1);
  DMA_setChannelTransfer(UDMA_ALT_SELECT | DMA_CH7_ADC14, UDMA_MODE_PINGPONG, (void*) &ADC14->MEM[0], data_u1, GM_SAMP_LEN);
  
//interrupt
  DMA_clearInterruptFlag(DMA_CHANNEL_7);
  DMA_assignInterrupt(_DMA_INT, DMA_CHANNEL_7);
  Interrupt_enableInterrupt(_INT_DMA_INT);
  
//start
  DMA_enableChannel(DMA_CHANNEL_7);
  ADC14_enableConversion();
#endif
}

void mcu_adc_en(void)
{
  ADC14_enableConversion();
}
//-----------------------------------------------------------------------------

//------------------------------------ISR--------------------------------------
void ADC14_IRQHandler(void)
{
#if 1
  if(ADC14->IFGR0 & ADC14_IFGR0_IFG1){
    ADC14->CLRIFGR0 |= ADC14_CLRIFGR0_CLRIFG1;
    
    static uint32_t   cnt;
    igaf_adc_u[cnt] = (float)(ADC14_getResult(ADC_MEM0))*(_ADC_VREF/16384);
    igaf_adc_i[cnt] = (float)(ADC14_getResult(ADC_MEM1))*(_ADC_VREF/16384);
    if(++cnt >= _SAMP_LEN){
      cnt = 0;
      igu32_bufflag = 1;
      //ADC14_disableConversion();
    }
  }
#endif
}

#if 0
void DMA_INT2_IRQHandler(void)
{
  //Switch between primary and alternate bufferes with DMA's PingPong mode
  if(DMA_getChannelAttribute(DMA_CHANNEL_7) & UDMA_ATTR_ALTSELECT){
    DMA_setChannelControl (UDMA_PRI_SELECT | DMA_CH7_ADC14, UDMA_SIZE_16 | UDMA_SRC_INC_NONE | UDMA_DST_INC_16 | UDMA_ARB_1);
    DMA_setChannelTransfer(UDMA_PRI_SELECT | DMA_CH7_ADC14, UDMA_MODE_PINGPONG, (void*) &ADC14->MEM[0], data_u0, _SAMP_LEN);
    
    igu32_bufflag = 1;
    for(uint32_t i=0; i<_SAMP_LEN; i++){
      igaf_adc[i] = (float)(data_u0[i])*(_ADC_VREF/16384);
    }
  }else{
    DMA_setChannelControl (UDMA_ALT_SELECT | DMA_CH7_ADC14, UDMA_SIZE_16 | UDMA_SRC_INC_NONE | UDMA_DST_INC_16 | UDMA_ARB_1);
    DMA_setChannelTransfer(UDMA_ALT_SELECT | DMA_CH7_ADC14, UDMA_MODE_PINGPONG, (void*) &ADC14->MEM[0], data_u1, _SAMP_LEN);
    
    igu32_bufflag = 2;
    for(uint32_t i=0; i<_SAMP_LEN; i++){
      igaf_adc[i] = (float)(data_u1[i])*(_ADC_VREF/16384);
    }
  }
}
#endif
//------------------------------------EOF--------------------------------------





